﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.General;
using PPI.WebConsumer.PPI.Svc;
using EXMissionStructures_PoleTopType=PPI.Entities.Lookup.EXMissionStructures_PoleTopType;

namespace PPI.WebConsumer.Distribution
{
    public class WDistributionPoleTop
    {
        public static DataSet SelectAllIdName(Int32 fromId)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            Service1 svc = new Service1();

            return svc.DistributionTops_SelectAllIdNameFromId(_Reader.ConectionMode, fromId);
        }

        public static DataSet SelectAllIdName()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            Service1 svc = new Service1();

            return svc.DistributionTops_SelectAllIdName(_Reader.ConectionMode);
        }

        public static List<EXMissionStructures_PoleTopType> Save(List<EXMissionStructures_PoleTopType> tops, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.EXMissionStructures_PoleTopType> wTops = new List<PPI.Svc.EXMissionStructures_PoleTopType>();
            foreach (EXMissionStructures_PoleTopType top in tops)
            {
                PPI.Svc.EXMissionStructures_PoleTopType t = new PPI.Svc.EXMissionStructures_PoleTopType();

                t.Id = top.Id;
                t.Key = top.Key;
                t.Name = top.Name;

                wTops.Add(t);
            }

            PPI.Svc.EXMissionStructures_PoleTopType[] wTopsResult = svc.DistributionTop_Save(poleId, wTops.ToArray(), reader.GetConnectionString());
            tops = new List<EXMissionStructures_PoleTopType>();
            foreach (PPI.Svc.EXMissionStructures_PoleTopType topResult in wTopsResult)
            {
                EXMissionStructures_PoleTopType tResp = new EXMissionStructures_PoleTopType();

                tResp.ServerId = topResult.ServerId;
                tResp.Id = topResult.Id;
                tResp.Key = topResult.Key;
                tResp.Name = topResult.Name;

                tops.Add(tResp);
            }

            return tops;
        }

        public static List<EXMissionStructures_PoleTopType> Update(List<EXMissionStructures_PoleTopType> tops, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.EXMissionStructures_PoleTopType> wTops = new List<PPI.Svc.EXMissionStructures_PoleTopType>();
            foreach (EXMissionStructures_PoleTopType top in tops)
            {
                PPI.Svc.EXMissionStructures_PoleTopType t = new PPI.Svc.EXMissionStructures_PoleTopType();

                t.Id = top.Id;
                t.Key = top.Key;
                t.Name = top.Name;

                wTops.Add(t);
            }

            PPI.Svc.EXMissionStructures_PoleTopType[] wTopsResult = svc.DistributionTop_Update(poleId, wTops.ToArray(), reader.GetConnectionString());
            tops = new List<EXMissionStructures_PoleTopType>();
            foreach (PPI.Svc.EXMissionStructures_PoleTopType topResult in wTopsResult)
            {
                EXMissionStructures_PoleTopType tResp = new EXMissionStructures_PoleTopType();

                tResp.ServerId = topResult.ServerId;
                tResp.Id = topResult.Id;
                tResp.Key = topResult.Key;
                tResp.Name = topResult.Name;

                tops.Add(tResp);
            }

            return tops;
        }
    }
}
